<template>
  <el-dialog
    :title="!dataForm.infoId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="发布账户" prop="pubDiscount">
      <el-input v-model="dataForm.pubDiscount" placeholder="发布账户"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="pubTime">
      <el-input v-model="dataForm.pubTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="咨询内容" prop="infoText">
      <el-input v-model="dataForm.infoText" placeholder="咨询内容"></el-input>
    </el-form-item>
    <el-form-item label="楼盘Id" prop="housesId">
      <el-input v-model="dataForm.housesId" placeholder="楼盘Id"></el-input>
    </el-form-item>
    <el-form-item label="类型id" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="类型id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          infoId: 0,
          title: '',
          pubDiscount: '',
          pubTime: '',
          infoText: '',
          housesId: '',
          typeId: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          pubDiscount: [
            { required: true, message: '发布账户不能为空', trigger: 'blur' }
          ],
          pubTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          infoText: [
            { required: true, message: '咨询内容不能为空', trigger: 'blur' }
          ],
          housesId: [
            { required: true, message: '楼盘Id不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '类型id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.infoId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.infoId) {
            this.$http({
              url: this.$http.adornUrl(`/floor/information/info/${this.dataForm.infoId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.information.title
                this.dataForm.pubDiscount = data.information.pubDiscount
                this.dataForm.pubTime = data.information.pubTime
                this.dataForm.infoText = data.information.infoText
                this.dataForm.housesId = data.information.housesId
                this.dataForm.typeId = data.information.typeId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/floor/information/${!this.dataForm.infoId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'infoId': this.dataForm.infoId || undefined,
                'title': this.dataForm.title,
                'pubDiscount': this.dataForm.pubDiscount,
                'pubTime': this.dataForm.pubTime,
                'infoText': this.dataForm.infoText,
                'housesId': this.dataForm.housesId,
                'typeId': this.dataForm.typeId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
